package com.why.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.why.pojo.ProductCategory;
import com.why.service.ProductCategoryService;
import com.why.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class ProductCategoryController {

    @Autowired
    ProductCategoryService productCategoryService;

    @Autowired
    ProductService productService;

    //查询一级、二级、三级分类和所有的商品类
    @RequestMapping(value = "procategorylistbytype",produces = "application/json;charset=utf-8")
    public String proCategoryListby(){
        List<List<ProductCategory>> categoryList = productCategoryService.proCategoryListByType();
        return JSON.toJSONString(categoryList);
    }

    //根据父级分类查询子分类
    @RequestMapping(value = "procategorylist",produces = "application/json;charset=utf-8")
    public String proCategoryList(Integer parentId){
        List<ProductCategory> categoryList = productCategoryService.proCategoryList(parentId);
        return JSON.toJSONString(categoryList);
    }

    //所有分类查询
    @RequestMapping(value = "allcategorylist",produces = "application/json;charset=utf-8")
    public String allCategoryList(Integer pageNum,Integer pageSize){
        PageInfo<ProductCategory> pageInfo = productCategoryService.allCategoryList(pageNum,pageSize);
        return JSON.toJSONString(pageInfo);
    }

    //添加分类
    @RequestMapping(value = "addcategory",produces = "application/json;charset=utf-8")
    public String addCategory(ProductCategory productCategory){
        boolean exist = productCategoryService.categoryExist(productCategory);
        if (exist){
            return JSON.toJSONString("exist");
        }
        boolean result = productCategoryService.insertSelective(productCategory);
        if (result){
            return JSON.toJSONString("success");
        }
        return JSON.toJSONString("failed");

    }

    //根据id分类查询
    @RequestMapping(value = "categorybyid",produces = "application/json;charset=utf-8")
    public String categoryById(Integer categoryId){
        ProductCategory productCategory = productCategoryService.selectByPrimaryKey(categoryId);
        return JSON.toJSONString(productCategory);
    }

    //根据id分类查询
    @RequestMapping(value = "categorybytype",produces = "application/json;charset=utf-8")
    public String categoryByType(Integer type){
        List<ProductCategory> productCategories = productCategoryService.categoryByType(type);
        return JSON.toJSONString(productCategories);
    }

    //修改分类
    @RequestMapping(value = "mdfcategory")
    public String mdfCategory(ProductCategory productCategory){
        if (productCategoryService.updateByPrimaryKey(productCategory)){
            return "success";
        }
        return "failed";
    }

    //根据id删除
    @RequestMapping(value = "delcategory",produces = "application/json;charset=utf-8")
    public String delCategory(Integer categoryId){
        if (productCategoryService.selectCount(categoryId)>0){
            return JSON.toJSONString("hasChildren");
        }else if (productService.productList(categoryId,1,5).getSize()>0){
            return JSON.toJSONString("hasProduct");
        }
        boolean result = productCategoryService.deleteByPrimaryKey(categoryId);
        return JSON.toJSONString(result);
    }

}
